package com.ruoyi.app.controller;

import com.ruoyi.app.interceptor.SessionUtil;
import com.ruoyi.cms.domain.CrmMember;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.employee.domain.CrmEmployee;
import com.ruoyi.repairs.domain.CrmRepairs;
import com.ruoyi.repairs.service.ICrmRepairsService;
import com.ruoyi.repairs.domain.CrmRepairsRecord;
import com.ruoyi.repairs.service.ICrmRepairsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 维修Controller
 * 
 * @author hct
 * @date 2022-02-11
 */
@RestController
@RequestMapping("/app/repairs")
public class AppRepairsController extends BaseController
{
    @Autowired
    private ICrmRepairsService crmRepairsService;
    @Autowired
    private ICrmRepairsRecordService crmRepairsRecordService;

    /**
     * 查询维修列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CrmRepairs crmRepairs)
    {
        startPage();
        List<CrmRepairs> list = crmRepairsService.selectCrmRepairsList(crmRepairs);
        return getDataTable(list);
    }


    /**
     * 获取维修详细信息
     */
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(crmRepairsService.selectCrmRepairsById(id));
    }

    /**
     * 新增维修
     */
    @PostMapping(value = "apply")
    public AjaxResult add( CrmRepairs crmRepairs)
    {
        CrmMember crmEmployee=SessionUtil.getLoginUser();
        crmRepairs.setMemberId(crmEmployee.getId());
        Date date=new Date();
        crmRepairs.setCreateTime(date);
        crmRepairs.setRepairCode(IdUtils.randomUUID());
        crmRepairs.setRepairStatus(0);
        CrmRepairsRecord crmRepairsRecord=new CrmRepairsRecord();
        crmRepairsRecord.setRepairStatus(0);
        crmRepairsRecord.setRepairId(crmRepairs.getId());
        crmRepairsRecord.setCreateTime(date);
        crmRepairsService.insertCrmRepairs(crmRepairs);
        crmRepairsRecordService.insertCrmRepairsRecord(crmRepairsRecord);
        return AjaxResult.success("发布成功");
    }


    /**
     * 删除维修
     */
    @PostMapping("/delete/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(crmRepairsService.deleteCrmRepairsById(id));
    }
}
